const Koa = require('koa');
const app = new Koa();


const { koaBody } = require('koa-body');
app.use(koaBody({
    multipart: true,
}))

const KoaRouter = require('koa-router');
const router = new KoaRouter();

const cors = require("koa2-cors");
app.use(
    cors({
        origin: (ctx) => {
            return "*";
        },
        maxAge: 5, //指定本次预检请求的有效期，单位为秒。
        allowMethods: ["GET", "POST", "OPTIONS"], //设置所允许的HTTP请求方法
        allowHeaders: ["Content-Type", "Authorization", "Accept"], //设置服务器支持的所有头信息字段
        //exposeHeaders: ["WWW-Authenticate", "Server-Authorization"], //设置获取其他自定义字段
    })
);

const News = require('./entity/news');
const TodoList = require('./entity/todoList');
const Users = require('./entity/user');

router.get('/todo/list', async (ctx, next) => {
    const todoLists = await TodoList.findAll({where: {
            status: 1
        }});
    console.log("todoLists", todoLists);
    ctx.body = todoLists;
})

router.get('/progress/list', async (ctx, next) => {
    const progressLists = await TodoList.findAll({where: {
        status: 2
    }});
    console.log("progressLists", progressLists);
    ctx.body = progressLists;
})


router.get('/complete/list', async (ctx, next) => {
    const completeLists = await TodoList.findAll({where: {
            status: 3
        }});
    console.log("completeLists", completeLists);
    ctx.body = completeLists;
})


router.get('/todo/count', async (ctx, next) => {
    const todoCount = await TodoList.count({where: {
            status: 1
        }});
    console.log("todoCount", todoCount);
    ctx.body = {todoCount};
})

router.get('/progress/count', async (ctx, next) => {
    const progressCount = await TodoList.count({where: {
            status: 2
        }});
    console.log("progressCount", progressCount);
    ctx.body = {progressCount};
})


router.get('/complete/count', async (ctx, next) => {
    const completeCount = await TodoList.count({where: {
            status: 3
        }});
    ctx.body = {completeCount};
})


router.get('/news/list', async (ctx, next) => {
    const newsLists = await News.findAll();
    ctx.body = newsLists;
})

router.post('/todo/change', async (ctx, next) => {
    console.log(ctx.request.body)
    const {id, status} = ctx.request.body;
    const todoStatus = await TodoList.update({ status:status }, {
        where: {
            id:id
        }
    });
    ctx.body = {todoStatus};
})

router.post('/todo/remove', async (ctx, next) => {
    // console.log(ctx.request.body)
    const {id} = ctx.request.body;
    const removeStatus = await TodoList.update({ status:0 }, {
        where: {
            id:id
        }
    });
    ctx.body = {removeStatus};
})

router.post('/login', async (ctx, next) => {
    const {username, password} = ctx.request.body;
    console.log(username, password)

    const userInfo = await Users.findOne({
        where: {
            username:username
        }
    });
    console.log(userInfo.password);
    console.log("password",password);
    if(password==userInfo.password){
        ctx.body = true;
    }else{
        ctx.body = false;
    }
})

router.post('/addtask', async (ctx, next) => {
    const {taskTitle, taskContent} = ctx.request.body;
    console.log(taskTitle, taskContent);
    const newsInfo = await TodoList.create({
        task_name: taskTitle,
        task_content: taskContent,
        img: 'http://localhost:3000/_nuxt/2.png',
    });
    ctx.body = true;
})



app.use(router.routes()).use(router.allowedMethods());

app.listen(8093, ()=>{
    console.log('server is starting')
})
